package cn.com.self.apis.portal.service.impl;


import cn.com.self.apis.portal.entity.SysUserEntity;
import cn.com.self.apis.portal.mapper.SysUserMapper;
import cn.com.self.apis.portal.service.intf.ISysUserService;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description
 */
@Service
public class SysUserService extends BaseServiceImpl<SysUserEntity, String> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public int deletes(List<Long> ids) {
        return sysUserMapper.deleteBatchIds(ids);
    }

    @Override
    public SysUserEntity checkUser(Map<String, Object> queryParams) {
        SysUserEntity info =  new SysUserEntity();
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryParams.keySet().forEach(key -> {
            if(("id").equals(key)){
                queryWrapper.eq("id", queryParams.get("id"));

            }else if("username".equals(key)) {
                queryWrapper.eq("username",queryParams.get("username"));
            }
        });
        info = sysUserMapper.selectOne(queryWrapper);

        return info;
    }

    @Override
    public SysUserEntity selectByOpenid(Long openid) {
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("openid", openid);
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUserEntity selectOne1(String id) {

        return sysUserMapper.selectOne1(id);
    }

    @Override
    public Page<SysUserEntity> selectPage(Map<String, Object> queryMap, Pageable page) {
        int total = sysUserMapper.selectPageTotal(queryMap);
        queryMap.put("page", (page.getPageNumber() - 1) * page.getPageSize());
        queryMap.put("size", page.getPageSize());
        List<SysUserEntity> list = sysUserMapper.selectPages(queryMap);
        //判断是否是汽车协会
        /*for(int i = 0; i <list.size();i++){
            if(StringUtils.isNotBlank(list.get(i).getCommunityCode())){
                if(list.get(i).getCommunityCode()=="-1"){
                    list.get(i).setCommunityName("汽车协会");
                }
            }
        }*/
        return new PageImpl(list, PageRequest.of(page.getPageNumber() - 1, page.getPageSize()), total);
    }


}
